package com.zhl.service;

import static org.springframework.data.mongodb.core.query.Criteria.where;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.zhl.dao.impl.EmployeeDaoImpl;
import com.zhl.model.Employee;

@Service
public class EmployeeService {
	
	@Autowired
	EmployeeDaoImpl employeeDaoImpl;
	
	public String doTest(){
		
		List<Employee> employees;
		//ApplicationContext ctx = new ClassPathXmlApplicationContext("springmongo-config.xml");
		//EmployeeDaoImpl employeeDaoImpl = (EmployeeDaoImpl) ctx.getBean("employeeDaoImpl");
		String result = "获取所有结束<br />";
		employees = employeeDaoImpl.find();
		System.out.println("当前数据库的内容：");
		for(Employee e : employees){
			System.out.println(e.toString());
			result += e.toString()+"<br />";
		}
		System.out.println("++++++++++++++++++++++获取所有结束++++++++++++++++++++++++++++++++++");
		
		
		result += "新增数据<br />";
		Employee 对象 = new Employee( null,"天王盖地虎","宝塔镇河妖", 45);
		employeeDaoImpl.insert(对象);
		employees = employeeDaoImpl.find();
		System.out.println("当前数据库的内容：");
		for(Employee e : employees){
			System.out.println(e.toString());
			result += e.toString()+"<br />";
		}
		
		System.out.println("++++++++++++++++++++++新增结束++++++++++++++++++++++++++++++++++");
		
		
		result += "查询一条数据<br />";
		Query query =  new Query(where("firstname").is("天王盖地虎"));
		employees= employeeDaoImpl.find(query);
		System.out.println("当前数据库的内容：");
		for(Employee e : employees){
			System.out.println(e.toString());
			result += e.toString()+"<br />";
		}
		System.out.println("++++++++++++++++++++++按条件获取结束++++++++++++++++++++++++++++++++++");
		

		result += "修改数据<br />";
		Update  up = new Update();
        up.set("firstname","国贸");
        Query updateQuery = new Query(where("firstname").is("天王盖地虎"));
        employeeDaoImpl.update(updateQuery, up);
        employees = employeeDaoImpl.find();
        System.out.println("当前数据库的内容：");
		for(Employee e : employees){
			System.out.println(e.toString());
			result += e.toString()+"<br />";
		}
		System.out.println("++++++++++++++++++++++更新结束++++++++++++++++++++++++++++++++++");
		
		
		result += "删除数据<br />";
		Query removeQuery = new Query(where("firstname").is("国贸"));
		employeeDaoImpl.delete(removeQuery);
		employees = employeeDaoImpl.find();
		System.out.println("当前数据库的内容：");
		for(Employee e : employees){
			System.out.println(e.toString());
			result += e.toString()+"<br />";
		}
		
		System.out.println("++++++++++++++++++++++删除结束++++++++++++++++++++++++++++++++++");
		
		return result;
	}
	
	
	public static void main(String[] args){
		System.out.println("我是main方法");
		EmployeeService a = new EmployeeService();
		ApplicationContext ctx = new ClassPathXmlApplicationContext("springmongo-config.xml");
		a.employeeDaoImpl = (EmployeeDaoImpl) ctx.getBean("employeeDaoImpl");
		a.doTest();
	}
	
	
	
}
